﻿<template>
<a-card :bordered="false" style="margin-bottom: 10px">
  <a-form ref="searchFormRef" name="advanced_search" class="ant-advanced-search-form" :model="searchFormState">
    <a-row :gutter="24">
    <a-col :span="6">
      <a-form-item name="schoolId" label="学校id">
		<a-input v-model:value="searchFormState.schoolId" placeholder="请输入学校id"></a-input>
      </a-form-item>
    </a-col>
    <a-col :span="6">
      <a-form-item name="content" label="公告内容">
        <a-input v-model:value="searchFormState.content" placeholder="请输入公告内容"></a-input>
      </a-form-item>
    </a-col>
    <a-col :span="6">
      <a-form-item name="creatTime" label="创建时间">
		<a-range-picker v-model:value="searchFormState.creatTime" value-format="YYYY-MM-DD HH:mm:ss" show-time />
      </a-form-item>
    </a-col>
    <a-col :span="6">
      <a-button type="primary" @click="table.refresh(true)">
        <template #icon><SearchOutlined /></template>查询</a-button>
      <a-button class="buttom-left" @click="() => searchFormRef.resetFields()">
        <template #icon><redo-outlined /></template>重置</a-button>
    </a-col>
    </a-row>
  </a-form>
</a-card>
<a-card :bordered="false">
  <s-table
    ref="table"
    :columns="columns"
    :data="loadData"
    :alert="options.alert.show"
    :scroll="{ x: '1000px' | true }"
    bordered
        :row-key="(record) => record.id"
    :row-selection="options.rowSelection"
    :tool-config="toolConfig"
  >
    <template #operator>
      <a-space>
        <a-button type="primary" @click="formRef.onOpen()"  v-if="hasPerm('buAnnouncementAdd')">
        <template #icon><plus-outlined /></template>新增公告</a-button>
      </a-space>
    </template>
    <template #bodyCell="{ column, record }">
      <template v-if="column.dataIndex === 'schoolId'">
         {{ fkBuSchoolOptions.find((item) => item.value === record.schoolId)?.label }}
      </template>
      <template v-if="column.dataIndex === 'action'">
        <a-space>
          <a @click="formRef.onOpen(record)" v-if="hasPerm('buAnnouncementEdit')" >编辑</a>
          <a-divider type="vertical"  v-if="hasPerm(['buAnnouncementEdit', 'buAnnouncementDelete'], 'and')" />
          <a-popconfirm title="确定要删除吗？" @confirm="deleteBuAnnouncement(record)">
            <a-button type="link" danger size="small"  v-if="hasPerm('buAnnouncementDelete')">删除</a-button>
          </a-popconfirm>
      </a-space>
      </template>
    </template>
  </s-table>
</a-card>
<Form ref="formRef" @successful="table.refresh(true)" />
</template>

<script setup name="announcement">
  import { message, Empty } from 'ant-design-vue'
  import Form from './form.vue'
  import buAnnouncementApi from '@/api/biz/buAnnouncementApi'
  
  let searchFormState = reactive({}) //查询表单状态
  const searchFormRef = ref() //查询表单
  // 查询区域显示更多控制
  const advanced = ref(false)
  const toggleAdvanced = () => {
      advanced.value = !advanced.value
  }
  const formRef = ref()//新增/编辑表单页
  


  //表格配置
  const table = ref() //表格
  let selectedRowKeys = ref([]) //选中项
  const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false } //表格工具配置
  const columns = [
      {
        title: '学校id',
        width: 100,
        dataIndex: 'schoolId'
      },
      {
        title: '公告内容',
        width: 100,
        dataIndex: 'content'
      },
      {
        title: '创建时间',
        width: 100,
        dataIndex: 'creatTime'
      },
  ]
  if (hasPerm(['buAnnouncementEdit', 'buAnnouncementDelete'])) {
	  columns.push({
		  title: '操作',
	  	  dataIndex: 'action',
	      align: 'center',
          fixed: 'right',
		  width: '130px'
	  })
  }
  
  // 列表选择配置
  const options = reactive({
		alert: {
			show: true, //显示选择多少个
			clear: () => {
				selectedRowKeys = ref([])
			}
		},
		rowSelection: {
			onChange: (selectedRowKey, selectedRows) => {
				selectedRowKeys.value = selectedRowKey
			}
		}
	}) 
  
  
  const fkBuSchoolOptions = ref([]) //学校id数据
  buAnnouncementApi.announcementFkBuSchoolSelector().then((data) => {
    fkBuSchoolOptions.value = data
  })
  
  
  // 表格查询 返回 Promise 对象
  const loadData = (parameter) => {
      const searchFormParam = JSON.parse(JSON.stringify(searchFormState))
      //creatTime范围查询条件重载
      if (searchFormParam.creatTime) {
          searchFormParam.startCreatTime = searchFormParam.creatTime[0]
          searchFormParam.endCreatTime = searchFormParam.creatTime[1]
          delete searchFormParam.creatTime
      }
      return buAnnouncementApi.announcementPage(Object.assign(parameter, searchFormParam)).then((data) => {
         return data })
    }
    
  // 删除
  const deleteBuAnnouncement = (record) => {
      let params = [
          {
            id: record.id ,
          }
      ]
      buAnnouncementApi.announcementDelete(params).then(() => {
        table.value.refresh(true)
      })
  }
   
  
  
  


</script>


<style scoped>
    .buttom-left {
      margin-left: 8px;
    }
</style>

